﻿var ScrambledEggs;
(function (ScrambledEggs) {
    (function (Modules) {
        var FirstMoves = (function () {
            function FirstMoves() {
                this._holdingSomething = false;
                this._feeding = 0;
            }
            FirstMoves.prototype.initialize = function (gameState, onUpdate) {
                var me = this;
                this._onUpdate = onUpdate;
                var moves = $("#firstmoves");
                moves.find("button.stare").click(function () {
                    gameState.State.MentalMaturity++;
                    $(this).hide();
                    setTimeout(function () {
                        gameState.State.Message = ".";
                        me._onUpdate();
                    }, 500);
                    setTimeout(function () {
                        gameState.State.Message = "..";
                        me._onUpdate();
                    }, 1000);
                    setTimeout(function () {
                        gameState.State.Message = "...";
                        me._onUpdate();
                    }, 1500);
                    setTimeout(function () {
                        gameState.State.Message = "You realised you have fingers!";
                        gameState.State.MentalMaturity += 6;
                        me._onUpdate();
                    }, 3000);
                });
                moves.find("button.finger").click(function () {
                    ++gameState.State.PhysicalMaturity;
                    switch(gameState.State.PhysicalMaturity) {
                        case 0:
                            gameState.State.Message = "Great, you moved it!";
                            break;
                        case 1:
                            gameState.State.Message = "Hmm!";
                            break;
                        case 2:
                            gameState.State.Message = "You're getting the hang of this!";
                            break;
                        case 3:
                            gameState.State.Message = "Keep up the good work!";
                            break;
                    }
                    me._onUpdate();
                });
                moves.find("button.grab").click(function () {
                    ++gameState.State.PhysicalMaturity;
                    me._feeding = 0;
                    if(Math.random() * 1000 < 650 + gameState.State.PhysicalMaturity * 2) {
                        me._holdingSomething = true;
                        gameState.State.Message = "You grab something";
                        me._onUpdate();
                    } else {
                        me._holdingSomething = false;
                        gameState.State.Message = "You grab nothing";
                        me._onUpdate();
                    }
                    ScrambledEggs.Cooldowns.Set("#firstmoves button.grab", 3);
                });
                moves.find("button.throw").click(function () {
                    if(gameState.State.DecisionCatFood == ScrambledEggs.CatFoodDecision.Reached) {
                        gameState.State.DecisionCatFood = ScrambledEggs.CatFoodDecision.Threw;
                        gameState.State.Message = "You <em>threw away</em> her food bowl in revenge! The Cat <i>=°ᴥ°=</i> won't soon forget this!";
                        me._onUpdate();
                        return;
                    }
                    if(gameState.State.Inventory["PenKey"] == -1) {
                        gameState.State.Message = "<em>You threw it away somewhere. I hope it wasn't anything important...</em>";
                        gameState.State.Inventory["PenKey"] = 0;
                        me._onUpdate();
                        return;
                    }
                    var hit = (Math.random() + "").substr(3, 5) % 10;
                    switch(hit) {
                        case 0:
                        case 1:
                            gameState.State.Message = "You <em>almost</em> hit The Cat. <i>=°ᴥ°=</i>.";
                        case 2:
                            gameState.State.Message = "<strong>You hit grandpa. Your mucus looks good on him. :)</strong>";
                            break;
                        case 3:
                        case 4:
                            gameState.State.Message = "You hit The Cat. <i>=°ᴥ°=</i> The Cat uses Mean Look.";
                            break;
                        default:
                            gameState.State.Message = "You tossed it out of the pen.";
                            break;
                    }
                    gameState.State.PhysicalMaturity++;
                    gameState.State.MentalMaturity++;
                    me._holdingSomething = false;
                    me._feeding = 0;
                    me._onUpdate();
                });
                moves.find("button.eat").click(function () {
                    if(gameState.State.DecisionCatFood == ScrambledEggs.CatFoodDecision.Reached) {
                        gameState.State.DecisionCatFood = ScrambledEggs.CatFoodDecision.Ate;
                        gameState.State.Fullness = 99;
                        gameState.State.Message = "You ate her food. The Cat <i>=°ᴥ°=</i> understands, but she's very annoyed!";
                        me._onUpdate();
                        return;
                    }
                    if(gameState.State.Inventory["PenKey"] == -1) {
                        gameState.State.Message = "<em>It tastes like iron and has the shape of a key.</em>";
                        gameState.State.Inventory["PenKey"] = 1;
                        me._onUpdate();
                        return;
                    }
                    var random = Math.floor((Math.random() * 100000)) % 5;
                    switch(random) {
                        case 0:
                        case 1:
                        case 2:
                            gameState.State.Message = "It's food!";
                            gameState.State.Fullness = ScrambledEggs.Utility.Number.Limit(gameState.State.Fullness + 35, 0, 100);
                            break;
                        case 3:
                            gameState.State.Message = "Tastes bad...";
                            if(gameState.State.MentalMaturity < 50) {
                                gameState.State.MentalMaturity += 2;
                            }
                            break;
                        case 4:
                            gameState.State.Message = "Tastes awful =/";
                            if(gameState.State.Fullness > 0) {
                                gameState.State.Fullness = ScrambledEggs.Utility.Number.Limit(gameState.State.Fullness - 5, 0, 100);
                            }
                            break;
                    }
                    me._holdingSomething = false;
                    me._feeding = 0;
                    me._onUpdate();
                });
                moves.find("button.cry").click(function () {
                    var button = $(this);
                    me._holdingSomething = false;
                    gameState.State.Message = "You start to cry...";
                    if(gameState.State.LeftPen) {
                        ScrambledEggs.Cooldowns.Lock();
                        setTimeout(function () {
                            gameState.State.Message = "It's baby feeding time!";
                            me._onUpdate();
                        }, 3000);
                        setTimeout(function () {
                            gameState.State.Message = "...?";
                            me._onUpdate();
                        }, 5000);
                        setTimeout(function () {
                            gameState.State.Message = "......?";
                            me._onUpdate();
                        }, 7000);
                        setTimeout(function () {
                            gameState.State.Message = ".........?!";
                            me._onUpdate();
                        }, 8000);
                        setTimeout(function () {
                            gameState.State.Message = "!!";
                            me._onUpdate();
                        }, 10000);
                        setTimeout(function () {
                            gameState.State.Message = "The Cat <i>=°ᴥ°=</i> has closed the door, they can't hear you!";
                            me._onUpdate();
                            ScrambledEggs.Cooldowns.Unlock();
                        }, 12000);
                        return;
                    }
                    ScrambledEggs.Cooldowns.Set("#firstmoves button.cry", 40);
                    ScrambledEggs.Cooldowns.SetIfGreater("#firstmoves button.grab", 8);
                    ScrambledEggs.Cooldowns.SetIfGreater("#play button", 16);
                    me._onUpdate();
                    me._feeding = 70;
                    setTimeout(function () {
                        gameState.State.Message = "It's baby feeding time!";
                        var interval = setInterval(function () {
                            if((gameState.State.Fullness += 2) >= 100) {
                                gameState.State.Fullness = Math.min(gameState.State.Fullness, 99);
                                clearInterval(interval);
                            }
                            me._onUpdate();
                        }, 300);
                    }, 3000);
                });
            };
            FirstMoves.prototype.update = function (gameState, triggerSource) {
                var me = this;
                var moves = $("#firstmoves");
                if(gameState.State.Phase != ScrambledEggs.GamePhase.Baby || gameState.State.DecisionCatFood >= ScrambledEggs.CatFoodDecision.Ate) {
                    moves.hide();
                    return;
                }
                moves.show();
                moves.find("button.stare").toggle(gameState.State.MentalMaturity == -1);
                moves.find("button.finger").toggle(gameState.State.MentalMaturity >= 5 && gameState.State.PhysicalMaturity <= 5);
                moves.find("button.grab").toggle(!me._holdingSomething && gameState.State.PhysicalMaturity >= 5 && gameState.State.MentalMaturity > 5 && gameState.State.Inventory["PenKey"] != -1 && gameState.State.DecisionCatFood != ScrambledEggs.CatFoodDecision.Reached);
                moves.find("button.throw").toggle(me._holdingSomething || gameState.State.Inventory["PenKey"] == -1 || gameState.State.DecisionCatFood == ScrambledEggs.CatFoodDecision.Reached);
                moves.find("button.eat").toggle(me._holdingSomething || gameState.State.Inventory["PenKey"] == -1 || gameState.State.DecisionCatFood == ScrambledEggs.CatFoodDecision.Reached);
                moves.find("button.cry").toggle(gameState.State.Fullness >= 0 && gameState.State.Inventory["PenKey"] != -1 && gameState.State.DecisionCatFood != ScrambledEggs.CatFoodDecision.Reached);
                if(triggerSource != me) {
                    me._feeding = 0;
                }
            };
            return FirstMoves;
        })();
        Modules.FirstMoves = FirstMoves;        
    })(ScrambledEggs.Modules || (ScrambledEggs.Modules = {}));
    var Modules = ScrambledEggs.Modules;
})(ScrambledEggs || (ScrambledEggs = {}));
